package cn.zn.framework.mybatis.methods;

import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.SqlSource;

/**
 * 查询下一个排序号
 *
 * @author zn
 */
public class GetMaxSort extends AbstractMethod {

    @Override
    public MappedStatement injectMappedStatement(Class<?> mapperClass, Class<?> modelClass, TableInfo tableInfo) {
        /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
        StringBuilder sql = new StringBuilder("select max(sort) from ")
                .append(tableInfo.getTableName())
                .append(" where ").append("${ew.sqlSegment}");
        /* mapper 接口方法名一致 */
        String method = "getMaxSort";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql.toString(), modelClass);
        return addSelectMappedStatementForTable(mapperClass, method, sqlSource, tableInfo);
    }
}
